package org.metasketch.editor.toolbox;

import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.BaseLink;
import org.metasketch.editor.toolbox.advanced.IDragSource;
import org.metasketch.editor.toolbox.advanced.IDropHandler;
import org.metasketch.javafx.graphics.JFXDiagram;
import org.metasketch.model.BaseModel;

public class DefaultToolInputHandler implements IInputHandler {

	protected SimpleToolBaseDiagramType diagramType;
	private IToolHandler toolHandler;
	protected IDragSource dragSource;
	private Object dropTarget;
	private IDropHandler dropHandler;

	public DefaultToolInputHandler(SimpleToolBaseDiagramType diagramType) {
		this.diagramType = diagramType;
		// this.dragSource = diagramType.get
	}

	public void setToolHandler(IToolHandler toolHandler) {
		if (this.toolHandler != null)
			this.toolHandler.cleanup();

		this.toolHandler = toolHandler;

	}

	@Override
	public void handle(MouseEvent event, JFXDiagram jfxDiagram, BaseModel model) {
		// System.out.println(event);

		// diagramType.cleanInterface();

		Object source = event.getSource();
		// System.out.println("Diagram Event: " + event);

		// handling drag'n'drops
		if (dragSource.isDragging() && dragSource.getSemantic() != null) {

			System.out.println("Diagram Event: " + event);
			// dropTarget = source;
			//
			if (event.getEventType() == MouseEvent.MOUSE_MOVED
					&& event.getButton() == MouseButton.NONE) {
				dropTarget = source;

				dropHandler.handleDrop(dragSource.getSemantic(), dropTarget,
						event, jfxDiagram, model);

				dragSource.stopDragging();

				event.consume();
				model.getModelChangeManager().alertChanges(dragSource.getSemantic());
				
				//
				// // System.out.println("Diagram Event: " + event);
				// dropTarget = source;
				// // event.consume();
				// // System.out.println(dragSource.getSemantic());
				// //
				// // dragSource.stopDragging();
				// }
			}
		}

		// if (!event.isConsumed()
		// && event.getEventType() == MouseEvent.MOUSE_RELEASED) {
		// System.out.println("Diagram Event: " + event);
		// }

		if (toolHandler != null)
			toolHandler.handle(event, jfxDiagram, model);

		if (toolHandler == null || !event.isConsumed()) {

			if (event.getEventType() == MouseEvent.MOUSE_CLICKED
					&& event.getButton() == MouseButton.SECONDARY) {
				if (source instanceof BaseGraphic || source instanceof BaseLink) {
					diagramType.processSecondaryMenu(event);
				}
			}

			// if (!event.isConsumed()) {
			//
			// if (source instanceof BaseGraphic) {
			// BaseGraphic sourceGraphic = (BaseGraphic) source;
			// sourceGraphic.handle(event);
			// }
			// }
		}

	}

	public void setDragSource(IDragSource dragSource) {
		this.dragSource = dragSource;

	}

	// public Object getDropTarget() {
	// return dropTarget;
	// }

	public void setDropHandler(IDropHandler dropHandler) {
		this.dropHandler = dropHandler;

	}

}
